package cn.ejiajunxy.manager.fragment;

import android.os.Bundle;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.leo.baseui.mutiType.base.Items;
import com.leo.baseui.mutiType.listFragment.ListFragment2;
import com.leo.utils.CheckUtils;

import cn.ejiajunxy.manager.adapter.MineAdapter;
import cn.ejiajunxy.manager.api.ApiService;
import cn.ejiajunxy.manager.common.AppObserver;
import cn.ejiajunxy.manager.model.HeadModel;
import cn.ejiajunxy.manager.model.MoneyModel;
import cn.ejiajunxy.manager.model.MyOrderModel;
import cn.ejiajunxy.manager.utils.ToastUtils;
import cn.ejiajunxy.manager.utils.UserPrefManager;

/**
 * Created by zhangguobao on 2018/3/17.
 */

public class FragmentMyOrder extends ListFragment2<MineAdapter> {
    private Items mMoney;
    private Items mHeads;
    private int pos = 1;
    private MineAdapter mMineAdapter;

    @Override
    public MineAdapter getAdapter() {
        return mMineAdapter;
    }

    @Override
    protected void preInit() {
        super.preInit();
        mMineAdapter = new MineAdapter(getContext());
    }

    public static FragmentMyOrder newInstance() {
        Bundle args = new Bundle();
        FragmentMyOrder fragment = new FragmentMyOrder();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onInit(Bundle var1) {
        mMoney = new Items();
        mHeads = new Items();
        mHeads.add(new HeadModel());
        setItems(mHeads);
        requestMoney();
        requestMyOrder();
    }


    @Override
    public void onLoadMore() {
//        requestMyOrder();
    }

    @Override
    public void onRefresh() {
        pos = 1;
        requestMoney();
        requestMyOrder();
    }

    private void requestMoney() {
        RxHttpUtils.createApi(ApiService.class)
                .getMoney(UserPrefManager.getToken(getActivity()))
                .compose(Transformer.<MoneyModel>switchSchedulers())
                .subscribe(new AppObserver<MoneyModel>() {
                    @Override
                    protected void onError(String s) {
                        ToastUtils.showNetError(getContext());
                    }

                    @Override
                    protected void success(MoneyModel orderModel) {
                        hideNoDataLoading();
                        setRefreshComplete();
                        if (orderModel == null || orderModel.status != 0 || orderModel.finance == null) {
                            ToastUtils.showToast(getContext(), "请求金额失败");
                            return;
                        }
                        if (CheckUtils.isNotEmpty(mMoney)) {
                            removeRangeItemsInScope(mHeads.size(), mMoney.size());
                            mMoney.clear();

                        }
                        mMoney.add(orderModel);
                        insertRangeItems(mMoney, mHeads.size());
                    }
                });
    }

    private void requestMyOrder() {
        RxHttpUtils.createApi(ApiService.class)
                .getMyOrder(UserPrefManager.getToken(getActivity()), pos, 20)
                .compose(Transformer.<MyOrderModel>switchSchedulers())
                .subscribe(new AppObserver<MyOrderModel>() {
                    @Override
                    protected void onError(String s) {
                        ToastUtils.showNetError(getContext());
                    }

                    @Override
                    protected void success(MyOrderModel orderModel) {
                        hideNoDataLoading();
                        if (orderModel == null || orderModel.status != 0) {
                            ToastUtils.showToast(getContext(), "请求订单失败");
                            return;
                        }
                        Items items = new Items();
                        items.addAll(orderModel.tradeList);
                        if (pos == 1) {
                            removeRangeItemsInScope(mHeads.size() + mMoney.size(), mMineAdapter.getItemCount());
                            insertRangeItems(items, mHeads.size() + mMoney.size());
                        } else {
                            addMoreItems(items);
                        }
                        pos++;
                        if (20 > items.size()) {
                            setLoadMoreOver();
                        } else {
                            setLoadMoreRestore();
                            setLoadMoreComplete();
                        }
                    }
                });
    }
}
